//
//  QDNewsTableHeaderView.m
//  QDMetroClient
//
//  Created by YueWen on 2019/2/18.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDNewsTableHeaderView.h"
#import "SDCollectionViewCell.h"
#import "QDMetroClient-Swift.h"
#import "RITLCollectionViewPageFlowLayout.h"

CGFloat QDNewsTableHeaderViewHeight(void) {
    
    CGFloat scale = 140.0 / 335;
    CGFloat itemWidth = UIScreen.mainScreen.ritl_width - 20 - 20;
    return (NSInteger)ceil(itemWidth * scale) + 16;
}


@interface QDNewsTableHeaderView()<RITLSDCycleScrollViewDataSource,SDCycleScrollViewDelegate>

/// 布局对象
@property (nonatomic, strong) RITLCollectionViewPageFlowLayout *flowLayout;
/// 顶部的轮播数据源
@property (nonatomic, copy, nullable)NSArray <QDCarouselItem *> *carouses;

@end


@implementation QDNewsTableHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self buildView];
    }
    return self;
}


- (void)buildView {

    self.carouses = @[];
    self.cycleView.autoScroll = false;
    self.cycleView.pageEnable = false;
    [self addSubview:self.cycleView];
    [self makeCycleViewConstraints:self.cycleView];
}


- (void)makeCycleViewConstraints:(RITLSDCycleScrollView *)view {
    
    if (!view.superview) { return; }
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(16);
    }];
}

#pragma mark - <RITLSDCycleScrollViewDataSource>

//- (Class)cycleViewCustomCollectionViewCellClass:(RITLSDCycleScrollView *)cycleView {
//    return SDCollectionViewCell.class;
//}

- (NSDictionary<NSString *,Class> *)cycleViewAllRegisterCellClasses:(RITLSDCycleScrollView *)cycleView {
    
    return @{@"cell": SDCollectionViewCell.class,
             @"heziCell": QDHDHZCycleCollectionViewCell.class};
}


- (NSString *)cycleViewIdentifer:(RITLSDCycleScrollView *)cycleView atIndexPath:(NSIndexPath *)indexPath {
    if (self.carouses.count <= indexPath.item) {
        return @"cell";
    }
    return self.carouses[indexPath.item].isBox ? @"heziCell" : @"cell";
}


- (void)cycleView:(RITLSDCycleScrollView *)cycleView setupCustomCell:(UICollectionViewCell<RITLSDCycleScrollViewCell> *)cell forIndex:(NSIndexPath *)indexPath {
    
    // 盒子的cell
    if ([cell isKindOfClass:QDHDHZCycleCollectionViewCell.class]) {
        
        QDHDHZCycleCollectionViewCell *heziCell = (QDHDHZCycleCollectionViewCell *)cell;
        
        // 设置盒子的值
        [heziCell.bannerView setPositionKey:self.carouses[indexPath.item].boxKey properties:self.hzProperties];

        //设置代理
        heziCell.bannerView.delegate = self.heziDelegate;
        return;
    }
    
    cell.imageView.layer.cornerRadius = 8;
    cell.imageView.clipsToBounds = true;
}




- (UICollectionViewLayout *)cycleScollViewcustomCollectionViewLayout:(RITLSDCycleScrollView *)cycleView {
    
    RITLCollectionViewPageFlowLayout *flowLayout = RITLCollectionViewPageFlowLayout.new;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = CGFLOAT_MIN;
    
    // 计算item的Size
    CGFloat scale = 140.0 / 335;
    CGFloat itemWidth = UIScreen.mainScreen.ritl_width - 20 - 20;
    flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth * scale);
    
    self.flowLayout = flowLayout;
    
    return flowLayout;
}


- (BOOL)cycleViewShouldResetCollectionWithCustomScrollPosition:(RITLSDCycleScrollView *)cycleView {
    return true;
}


- (UICollectionViewScrollPosition)cycleViewCustomCollectionViewScrollPosition:(RITLSDCycleScrollView *)cycleView {
    return UICollectionViewScrollPositionCenteredHorizontally;
}


- (NSInteger)cycleView:(RITLSDCycleScrollView *)cycleView currentIndexWithContentOffset:(CGPoint)contentOffset {
    return [self.flowLayout itemAtIndexPathForCollectionWithContentOffsetX:contentOffset.x];
}


- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    [self.actor qdNewsTableHeaderView:self bannerAtIndex:index];
}


#pragma mark - Lazy

- (RITLSDCycleScrollView *)cycleView {
    
    if (!_cycleView) {
        
        RITLSDCycleScrollView *view = [RITLSDCycleScrollView new];
        view.dataSource = self;
        view.delegate = self;
        view.backgroundColor = UIColor.whiteColor;
        
        _cycleView = view;
    }
    
    return _cycleView;
}



#pragma mark - Update

- (void)updateWithItems:(__kindof NSArray<QDCarouselItem *> *)carouses
               complete:(void (^)(QDNewsTableHeaderView * _Nonnull))complete
{
    //进行赋值
    self.carouses = carouses;

    //进行UI设置
    self.cycleView.autoScroll = carouses.count > 1;
    self.cycleView.infiniteLoop = carouses.count > 1;
    self.cycleView.imageURLStringsGroup = [carouses ritl_map:^id _Nonnull(QDCarouselItem * _Nonnull obj) {
        return obj.imgUrl;
    }];

    if (complete) {
        complete(self);
    }
}


@end
